class Solution(object):
    def removeDuplicates(self, s, k):
        st = []
        for c in s:
            if st:
                if st[-1][0] == c:
                    st[-1][1] += 1
                    if st[-1][1] == k:
                        st.pop()
                else:
                    st.append([c, 1])
            else:
                st.append([c, 1])
        st = [t[0] * t[1] for t in st]
        return ''.join(st)
